package com.ldzy.controller;

import com.github.pagehelper.Page;
import com.ldzy.pojo.Result;
import com.ldzy.pojo.ResultCode;
import com.ldzy.pojo.Room;
import com.ldzy.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


@Controller
public class RoomController {
    @Autowired
    RoomService roomService;

    @RequestMapping("/selectNewRooms")//内容展示区域
    public ModelAndView selectNewRooms(Integer pageNum, Integer pageSize) {
        if (pageNum == null) pageNum = 1;
        if (pageSize == null) pageSize = 5;
        Page<Room> rooms = roomService.selectNewRooms(pageNum, pageSize, null);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("rooms_new");
        modelAndView.addObject("pageResult", rooms.getResult());
        modelAndView.addObject("myTotal", rooms.getTotal());
        modelAndView.addObject("myPageSize", rooms.getPageSize());
        modelAndView.addObject("pageNum", rooms.getPageNum());
        modelAndView.addObject("gourl", "/selectNewRooms");
        return modelAndView;
    }

    @RequestMapping("/search")//房间入住，查询所有房间，返回rooms页面
    public ModelAndView search(Integer pageNum, Integer pageSize, Room room) {
        System.out.println(pageNum + ":" + pageSize + ":" + room);
        if (pageNum == null) pageNum = 1;
        if (pageSize == null) pageSize = 5;
        System.out.println(room.getRoomname());
        Page<Room> rooms = roomService.selectNewRooms(pageNum, pageSize, room);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("rooms");
        modelAndView.addObject("pageResult", rooms.getResult());
        modelAndView.addObject("myTotal", rooms.getTotal());
        modelAndView.addObject("myPageSize", rooms.getPageSize());
        modelAndView.addObject("pageNum", rooms.getPageNum());
        modelAndView.addObject("gourl", "/search");
        modelAndView.addObject("search", room);
        return modelAndView;
    }

    @RequestMapping("/searchBorrowed")//查询已入住的房间，返回room_borrowed
    public ModelAndView searchBorrowed(Room room,Integer pageNum, Integer pageSize) {
        if (pageNum == null) pageNum = 1;
        if (pageSize == null) pageSize = 4;
        Page<Room> rooms = roomService.selectBorrowedRooms(pageNum, pageSize, room);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("room_borrowed");
        modelAndView.addObject("pageResult", rooms.getResult());
        modelAndView.addObject("myTotal", rooms.getTotal());
        modelAndView.addObject("myPageSize", rooms.getPageSize());
        modelAndView.addObject("pageNum", rooms.getPageNum());
        modelAndView.addObject("gourl", "/searchBorrowed");
        modelAndView.addObject("search", room);
        return modelAndView;
    }

    @RequestMapping("/findById")//根据id查找，返回房间信息
    @ResponseBody
    public Result<Room> findById(int id) {
        Room room = roomService.findById(id);
        if (room == null) {
            return new Result<>(false, ResultCode.ERROR_QUERY_GOODS.getDesc(), ResultCode.ERROR_QUERY_GOODS.getCode());
        } else {
            return new Result<>(true, ResultCode.SUCCESS_SELECT.getDesc(), ResultCode.SUCCESS_SELECT.getCode(), room);
        }
    }

    @RequestMapping("/borrowRoom")//入住保存
    @ResponseBody
    public Result borrowRoom(Room room) {
        room.setStatus("1");
        roomService.edit(room);
        Result result = new Result(true, ResultCode.SUCCESS.getDesc(), ResultCode.SUCCESS.getCode(), room);
        return result;
    }

    @RequestMapping("/returnRoom")//退房执行
    @ResponseBody
    public Result returnRoom(int id) {
        Room room=roomService.returnRoom(id);
        if (room == null) {
            return new Result<>(false, ResultCode.ERROR.getDesc(), ResultCode.ERROR.getCode());
        } else {
            return new Result<>(true, ResultCode.SUCCESS.getDesc(), ResultCode.SUCCESS.getCode(), room);
        }
    }
}